# -*- coding = utf-8 -*-
# @Time : 2022/11/17 11:07
# @Author : GHHHHHHHHH
# @File : ViewController.py
# @Software : PyCharm
import json
from flask import request, jsonify, Blueprint
from service import ViewService
from utils import Token

view = Blueprint('view', __name__)


@view.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, HEAD, OPTIONS')
    response.headers.add('Access-Control-Allow-Headers', '*')
    return response


@view.before_request
def is_login():
    return Token.verify_auth_token(request)


@view.route('/view/handleTrainLog', methods=['GET', 'POST'])
def handleTrainLog():
    data = request.get_data()
    data = json.loads(data)
    return ViewService.handleTrainLog(data)


@view.route('/view/handleLayerLog', methods=['GET', 'POST'])
def handleLayerLog():
    data = request.get_data()
    data = json.loads(data)
    return ViewService.handleLayerLog(data)


@view.route("/view/handlePreLog", methods=['GET', 'POST'])
def handlePreLog():
    data = request.get_data()
    data = json.loads(data)
    return ViewService.handlePreLog(data)


@view.route('/view/getAllLayerLog', methods=['GET', 'POST'])
def get_all_layer_logs():
    data = request.get_data()
    data = json.loads(data)
    uuid = data['uuid']
    return jsonify(ViewService.get_all_layer_log(uuid))


@view.route('/view/getAllTrainLog', methods=['GET', 'POST'])
def get_all_train_logs():
    data = request.get_data()
    data = json.loads(data)
    uuid = data['uuid']
    return jsonify(ViewService.get_all_train_log(uuid))


@view.route('/view/getAllPreLog', methods=['GET', 'POST'])
def get_all_pre_logs():
    data = request.get_data()
    data = json.loads(data)
    uuid = data['uuid']
    return jsonify(ViewService.get_all_pre_log(uuid))


@view.route('/view/saveTrainLog', methods=['GET', 'POST'])
def save_train_log():
    uuid = int(request.form.get('uuid', type=str, default=None))
    # 图表年份，默认2022
    data1 = request.files.get('file')
    data = {
        "uuid": uuid,
        "data": data1
    }
    return jsonify({"SUCCESS": True}) if ViewService.save_train_log(data) else jsonify({"SUCCESS": False})


@view.route('/view/saveLayerLog', methods=['GET', 'POST'])
def save_layer_log():
    uuid = int(request.form.get('uuid', type=str, default=None))
    # 图表年份，默认2022
    data1 = request.files.get('file')
    data = {
        "uuid": uuid,
        "data": data1
    }
    return jsonify({"SUCCESS": True}) if ViewService.save_layer_log(data) else jsonify({"SUCCESS": False})


@view.route('/view/savePreLog', methods=['GET', 'POST'])
def save_per_log():
    uuid = int(request.form.get('uuid', type=str, default=None))
    data1 = request.files.get('file')
    data = {
        "uuid": uuid,
        "data": data1
    }
    return jsonify({"SUCCESS": True}) if ViewService.save_pre_log(data) else jsonify({"SUCCESS": False})


@view.route('/view/deleteTrainLog', methods=['GET', 'POST'])
def deleteTrainLog():
    data = request.get_data()
    data = json.loads(data)
    return jsonify({"SUCCESS": True}) if ViewService.deleteTrainLog(data['id']) else jsonify({"SUCCESS": False})


@view.route('/view/deleteLayerLog', methods=['GET', 'POST'])
def deleteLayerLog():
    data = request.get_data()
    data = json.loads(data)
    return jsonify({"SUCCESS": True}) if ViewService.deleteLayerLog(data['id']) else jsonify({"SUCCESS": False})


@view.route('/view/deletePreLog', methods=['GET', 'POST'])
def deletePreLog():
    data = request.get_data()
    data = json.loads(data)
    return jsonify({"SUCCESS": True}) if ViewService.deletePreLog(data['id']) else jsonify({"SUCCESS": False})


@view.route('/view/getList', methods=['GET', 'POST'])
def getList():
    data = request.get_data()
    data = json.loads(data)
    return ViewService.getList(data['uuid'])
